C语言学习2:sizeof ,数据类型,scanf,算式操作,赋值与比较操作,逻辑操作,位的操作,条件运算符,特异赋值结果,强制转换,使用变量要注意的
1,sizeof用法以及各个类型占据的字节大小
#include <stdio.h> int main(void) { char ch; int i; long l; long long ll; float f; double d; long double ld; printf("size of char : %u\n", sizeof(ch)); printf("size of int : %u\n", sizeof(int)); printf("size of long : %u\n", sizeof l); printf("size of long long : %u\n", sizeof(long long)); printf("size of float : %u\n", sizeof f); printf("size of double : %u\n", sizeof d); printf("size of long double: %u\n", sizeof ld); return 0; }
结果:
size of char : 1
size of int : 4
size of long : 4
size of long long : 8
size of float : 4
size of double : 8
size of long double: 12
#include <stdio.h> // 字面值常量 // 字符型字面值: 'a' // 整型字面值: // 十进制: 12, +123, -126, 0, 16UL // 十六进制: 0x0a6f // 八进制: 0462 // 浮点型字面值: // 3.14 -2.56 // -3.14e-2 2.56e3 // 字符串字面值: // "china unix" int main(void) { char ch = 'a'; int a = 32; float f = 3.14e-2; double d = 2.567e2; printf("ch = %c\n", ch); printf("a = %d\n", a); printf("f = %f\n", f); printf("d = %lf\n", d); printf("------------\n"); ch = 98; printf("ch = %c\n", ch); return 0; }
结果:
ch = a
a = 32
f = 0.031400
d = 256.700000
------------
ch = b
3,scanf用法
#include <stdio.h> int main(void) { int a = 3; printf("a = %d\n", a); printf("addr of a = %p\n", &a); printf("pls input a num: "); scanf("%d", &a); printf("a = %d\n", a); return 0; }
结果:
a = 3
addr of a = 0xbfdca58c
pls input a num: 5648
a = 5648
4.算术操作
#include <stdio.h> /* arithmatic operators */ /* * / % + - */ int main(void) { double ret, x, y, z; printf("pls input x, y, z: "); scanf("%lf%lf%lf", &x, &y, &z); ret = 3 * x + 4 * y - 5 * z; printf("ret = %lf\n", ret); return 0; }
结果:
pls input x, y, z: 23,24,67
ret = 69.000000
5,比较操作
#include <stdio.h> /* compare operators */ /* > < >= <= == != */ int main(void) { int a = 3, b = 5; int ret; ret = a > b; printf("ret = %d\n", ret); ret = a < b; printf("ret = %d\n", ret); return 0; }
结果:
ret = 0
ret = 1
6,逻辑操作
#include <stdio.h> /* logic operators */ /* ! && || */ /* 逻辑运算符的左右操作数具有求值顺序 */ int main(void) { int a = 3, b = 5; int ret; ret = a && b; printf("ret = %d\n", ret); ret = !a; printf("ret = %d\n", ret); ret = a < b && a > b; printf("ret = %d\n", ret); ret = a < b || a > b; printf("ret = %d\n", ret); return 0; }
结果:
ret = 1
ret = 0
ret = 0
ret = 1
7,位操作
#include <stdio.h> /* bit operators */ /* ~ << >> & | ^ */ int main(void) { int a = 3; int ret; /* 左移 低位补0 */ ret = a << 2; printf("ret = %d, a = %d\n", ret, a); a = -3; ret = a << 2; printf("ret = %d, a = %d\n", ret, a); /* 右移 */ /* 有符号数, 视最高位补 */ /* 无符号数, 补0 */ ret = a >> 2; printf("ret = %d, a = %d\n", ret, a); unsigned u = -3; printf("u = %u\n", u); ret = u >> 2; printf("ret = %u, u = %u\n", ret, u); printf("----------------\n"); /* 按位取反 */ // 2 ^ 32 ==> 0 // 1 0000 0000 0000 0000 0000 0000 0000 0000 // 2 ^ 32 - 1 ==> -1 // 1111 1111 1111 1111 1111 1111 1111 1111 // 0000 0000 0000 0000 0000 0000 0000 0101 // ---------------------------------------- // 1111 1111 1111 1111 1111 1111 1111 1010 a = 5; ret = ~a; printf("ret = %d, a = %d\n", ret, a); printf("----------------\n"); /* 按位与 */ int b = 7; ret = a & b; printf("ret = %d\n", ret); printf("----------------\n"); /* 按位或 */ ret = a | b; printf("ret = %d\n", ret); printf("----------------\n"); /* 按位异或 相异为1 */ ret = a ^ b; printf("ret = %d\n", ret); return 0; }
结果:
ret = 12, a = 3
ret = -12, a = -3
ret = -1, a = -3
u = 4294967293
ret = 1073741823, u = 4294967293
----------------
ret = -6, a = 5
----------------
ret = 5
----------------
ret = 7
----------------
ret = 2
8,条件运算符
#include <stdio.h> /* ? : */ int main(void) { int a = 3, b = 5; int min = a > b ? b : a; int max = a > b ? a : b; printf("max = %d, min = %d\n", max, min); return 0; }
结果:
max = 5, min = 3
9,特异赋值结果,这些赋值都是不正确的
#include <stdio.h> int main(void) { int a, b, c; int ret; ret = a = 3, b = 5, c = 75; printf("ret = %d, a = %d, b = %d, c = %d\n", ret, a, b, c); printf("-------------------\n"); ret = (a = 3, b = 5, c = 75); printf("ret = %d, a = %d, b = %d, c = %d\n", ret, a, b, c); return 0; }
结果:
ret = 3, a = 3, b = 5, c = 75
-------------------
ret = 75, a = 3, b = 5, c = 75
10.强制转换类型
#include <stdio.h> int main(void) { int a = 5; double d = 3.14; double ret; ret = a + d; printf("ret = %lf\n", ret); ret = a + (int)d; printf("ret = %lf\n", ret); return 0; }
结果:
ret = 8.140000
ret = 8.000000
11,使用变量要小心的地方
#include <stdio.h> int main(void) { char ch = -1; unsigned char uch = -1; int a; /* 符号位扩展 */ a = ch; printf("a = %d, ch = %d\n", a, ch); ch = 8; a = ch; printf("a = %d, ch = %d\n", a, ch); /* 零扩展 */ a = uch; printf("a = %d, uch = %d\n", a, uch); printf("----------------\n"); /* 截断 */ a = 312; ch = a; printf("ch = %d, a = %d\n", ch, a); printf("----------------\n"); /* 溢出 */ unsigned char uch1 = 136, uch2 = 149; a = uch1 + uch2; printf("a = %d\n", a); return 0; }
结果:
a = -1, ch = -1
a = 8, ch = 8
a = 255, uch = 255 //-1只能是超出无符号的范围只能是255了
----------------
ch = 56, a = 312 //312-256=56,这个就是超出256的截断
----------------
a = 285 //溢出就是数据太大了